package com.rrqs.activity.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rrqs.activity.data.GetAbnormalReasonBean;
import com.rrqs.activity.data.OrderWaitData;
import com.zadtek.xmqishi.R;

import java.util.ArrayList;
import java.util.List;

public class ExceptionDialogAdapter extends RecyclerView.Adapter<ExceptionDialogAdapter.ViewHolder> {
    private Context mContext;
    private List<GetAbnormalReasonBean.ListBean> data;
    private onRecyclerItemClickerListener listener;
    private View lastClickView;
    private List<String> ids = new ArrayList<>();

    public ExceptionDialogAdapter(Context mContext, List<GetAbnormalReasonBean.ListBean> data) {
        this.mContext = mContext;
        this.data = data;
    }

    public void setData(List<GetAbnormalReasonBean.ListBean> data) {
        this.data = data;
    }

    public void setListener(onRecyclerItemClickerListener listener) {
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_exception_reason_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.abnormalReasonTv.setText(data.get(position).getMark_value());
        holder.abnormalReasonTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (lastClickView != null) {
                    holder.abnormalReasonTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                    lastClickView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.white_round_corner_bg));
                    lastClickView = view;
                }
                view.setBackground(ContextCompat.getDrawable(mContext, R.drawable.orange_round_corner_bg));
                holder.abnormalReasonTv.setTextColor(ContextCompat.getColor(mContext, R.color.white));

                if (ids.contains(data.get(position).getMark_value())) {
                    ids.remove(data.get(position).getMark_value());
                    holder.abnormalReasonTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                    holder.abnormalReasonTv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.white_round_corner_bg));
                } else {
                    ids.add(String.valueOf(data.get(position).getMark_value()));
                }

                listener.onRecyclerItemClick(position, ids);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView abnormalReasonTv;

        public ViewHolder(View itemView) {
            super(itemView);
            abnormalReasonTv = itemView.findViewById(R.id.abnormalReasonTv);

        }
    }

    /**
     * 点击监听回调接口
     */
    public interface onRecyclerItemClickerListener {
        void onRecyclerItemClick(int position, List<String> ids);
    }
}
